package com.dream.utilservice.utils;

import com.dream.utilservice.entity.SysFiled;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class PoiUtil {

    public static List<SysFiled> product2(String filepath) throws IOException {
        List<SysFiled> list = new ArrayList<>();
        InputStream is = new FileInputStream(filepath);
        //获取文档
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFTable> tables = doc.getTables();
        List<XWPFTableRow> rows=null;
        List<XWPFTableCell> cells;
        //所有的表格
        for (XWPFTable x:tables) {
            //获取表格的每一行
            rows = x.getRows();
            for (XWPFTableRow s:rows) {
                StringBuffer sb = new StringBuffer();
                cells =  s.getTableCells();
                for (XWPFTableCell c: cells) {
                    sb.append(c.getText()+"ofo");
                }
                String [] strins = sb.toString().split("ofo");
//                String length="0";
//                String type=strins[1];
//                String isval="";
//                if(strins[2].equals("N")){
//                    isval="Y";
//                }else if(strins[2].equals("Y")){
//                    isval="N";
//                }
                SysFiled sysFiled = new SysFiled();
                sysFiled.setField(strins[0]);
                sysFiled.setIsNotNull(strins[2]);
                list.add(sysFiled);
            }
        }

        return list;
    }



    public Set<String> product(String filepath) throws IOException {
        InputStream is = new FileInputStream(filepath);
        //获取文档
        XWPFDocument doc = new XWPFDocument(is);
        List<XWPFTable> tables = doc.getTables();
        List<XWPFTableRow> rows=null;
        List<XWPFTableCell> cells;
        int num = 0;
        String name = "kh";
        Set<String> set = new HashSet<>();
        //所有的表格
        for (XWPFTable x:tables) {
            //获取表格的每一行
            rows = x.getRows();
            for (XWPFTableRow s:rows) {
                num++;
                String index = name+num;
                 StringBuffer sb = new StringBuffer();
                cells =  s.getTableCells();
                for (XWPFTableCell c: cells) {
                    sb.append(c.getText()+"ofo");
                }
                String [] strins = sb.toString().split("ofo");
                String length="0";
                String type=strins[1];
                String isval="";
                if(strins[1].contains("(")){
                    int star = strins[1].indexOf("(");
                    int end = strins[1].indexOf(")");
                     length = strins[1].substring(star+1, end);
                     type = strins[1].substring(0, star);
                }
                if(strins[2].equals("N")){
                    isval="Y";
                }else if(strins[2].equals("Y")){
                    isval="N";
                }
                set.add(","+"'"+strins[0]+"'"+","+"'"+type+"'"+","
                        +length+","
                        +"'"+strins[4]+"'"+","
                        +"null"+","
                        +"null"+","
                        +"'"+"0"+"'"+","
                        +"'"+"3"+"'"+","
                        +"'"+strins[3]+"'"+","
                        +"'"+isval+"'"+","
                        +"'"+strins[2]+"'"+")"+",");
//                System.out.println("("+"'"+index+"'"+","+"'"+strins[0]+"'"+","+"'"+type+"'"+","
//                        +length+","
//                        +"'"+strins[4]+"'"+","
//                        +"null"+","
//                        +"null"+","
//                        +"'"+"0"+"'"+","
//                        +"'"+"3"+"'"+","
//                        +"'"+strins[3]+"'"+","
//                        +"'"+isval+"'"+","
//                        +"'"+strins[2]+"'"+")"+",");
            }
        }
        return set;
    }
    public static void main(String[] args) throws IOException {
        //doc文档路径
        String filePath = "C:\\Users\\user\\Desktop\\获取表格内容文档.docx";
       new PoiUtil().product2(filePath);

    }




}
